//
//  ForCommentTableViewCell.m
//  GoodBuy
//
//  Created by dlios on 15-5-15.
//  Copyright (c) 2015年 GoodBuy. All rights reserved.
//

#import "ForCommentTableViewCell.h"
#import "UIImageView+WebCache.h"

@implementation ForCommentTableViewCell

- (void)dealloc
{
    [_comment_ID release];
    [_comment_date release];
    [_author release];
    [_comment_content release];
    [super dealloc];
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
    
        self.author = [[UILabel alloc]init];
//        _author.backgroundColor = [UIColor whiteColor];
        [_author setFont:[UIFont systemFontOfSize:15]];
        [self.contentView addSubview:_author];
        
        _author.layer.shadowColor = [UIColor whiteColor].CGColor;


        [_author release];
        
        self.head = [[UIImageView alloc] init];
        [self .contentView addSubview:_head];
        [_head release];
        
        self.floor = [[UILabel alloc] init];
        _floor.backgroundColor = [UIColor whiteColor];
        _floor.numberOfLines = 0;
//        _floor.backgroundColor = [UIColor yellowColor];
        [_floor setFont:[UIFont systemFontOfSize:15]];
        [self.contentView addSubview:_floor];
        [_floor release];

        
        self.format_date = [[UILabel alloc] init];
        _format_date.backgroundColor = [UIColor whiteColor];
        [_format_date setFont:[UIFont systemFontOfSize:10]];
        _format_date.numberOfLines = 0;
        [self.contentView addSubview:_format_date];
        [_format_date release];
        
        self.comment_content = [[UIWebView alloc] init];
        _comment_content.delegate = self;
        _comment_content.backgroundColor = [UIColor whiteColor];
        _comment_content.scrollView.scrollEnabled = NO;

        _comment_content.opaque = NO;
        [self.contentView addSubview:_comment_content];
    
    }
    return self;
}

//设置 cell的空间Frame
- (void)layoutSubviews
{
    // 1. 调用父类的方法
    [super layoutSubviews];
    // 2.设置子视图Frame
    CGFloat F = [[self class] heightWithText:_author.text];
    
    _head.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/24, [UIScreen mainScreen].bounds.size.height/30, self.contentView.frame.size.width/6, 3*F);
    _author.frame = CGRectMake(_head.frame.origin.x + _head.frame.size.width + 5,_head.frame.size.height/2, _head.frame.size.width*1.7,F);
    
    
    _floor.frame = CGRectMake(_head.frame.size.width*5, _head.frame.origin.y, _head.frame.size.width,F);
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.from_client.frame = CGRectMake(_head.frame.size.width + _author.frame.size.width*1.3, _head.frame.size.width/2, _head.frame.size.width, F);
//    NSLog(@"%f",_head.frame.size.width + _author.frame.size.width*1.3);
    
    _format_date.frame = CGRectMake(_head.frame.size.width*5, _head.frame.size.height/1.5, _head.frame.size.width, F);

    
    
}
//wabView 自适应高度
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    CGFloat height = [ForCommentTableViewCell heightWithText:_stu.comment_content];
    _comment_content.frame = CGRectMake(_head.frame.origin.x , _head.frame.origin.y + _author.frame.size.height* 3.5, _head.frame.size.width * 5 , height + 10);
    
//     _comment_content.frame = CGRectMake(_head.frame.size.width + 20, , self.contentView.frame.size.width/2,height);
    
}


+(CGFloat)heightWithText:(NSString *)text
{
    //    设置一个字典接受
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    //    预设一个尺寸
    CGSize size = CGSizeMake(375 / 2, 1000);
    
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //    返回高度
    return rect.size.height;
}

//重写set方法

-(void)setStu:(ForCommentObject *)stu
{
    if (_stu != stu) {
        [_stu release];
        _stu = [stu retain];
    }
    _author.text = stu.comment_author;
    [_head sd_setImageWithURL:[NSURL URLWithString:stu.head]];
    _floor.text = stu.floor;
    _format_date.text = stu.format_date;

    [_comment_content loadHTMLString:stu.comment_content baseURL:nil];
    
}
@end
